﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ValidationFramework.Web.ASP;

namespace ValidationFramework.Web.Tests
{
	[TestFixture]
	public class ValidatorGeneratorTestFixture
	{
		[Test]
		public void ValidatorGeneratorTest()
		{
			var panel = new Panel();

			var myFooStringControl = new TextBox { ID = "myFooStringControl" };
			panel.Controls.Add(myFooStringControl);

			var myFooDateTimeControl = new TextBox { ID = "myFooDateTimeControl" };
			panel.Controls.Add(myFooDateTimeControl);

			var myFooDecimalControl = new TextBox { ID = "myFooDecimalControl" };
			panel.Controls.Add(myFooDecimalControl);

			var validatorGenerator = new PropertyValidatorGenerator();
			validatorGenerator.AddAssociation(myFooStringControl, "FooString");
			validatorGenerator.AddAssociation(myFooDecimalControl, "FooDecimal");
			validatorGenerator.AddAssociation(myFooDateTimeControl, "FooDateTime");
			validatorGenerator.TypeToValidate = typeof(ValidatorGeneratorTestObject).TypeHandle;
			validatorGenerator.GenerateValidators();
			Assert.AreEqual(29, panel.Controls.Count);
		}
	}
	public class ValidatorGeneratorTestObject
	{


		[RequiredStringRule]
		[LengthStringRule(5)]
		[RangeStringRule("aaaaa", "ccccc")]
		[RegexValidatorAttribute("bbbbb", ErrorMessage = "fooString must be bbbbb")]
		[CompareStringRule("bbbbb", CompareOperator.Equal)]
		public string FooString
		{
			get;
			set;
		}

		[RequiredDecimalRule]
		[RequiredDecimalRule(InitialValue = 2)]
		[CompareDecimalRule(123, CompareOperator.Equal)]
		public decimal FooDecimal
		{
			get;
			set;
		}

		[CompareDateTimeRule("10 Jan 1999", CompareOperator.Equal)]
		[RequiredDateTimeValidatorAttribute]
		public DateTime FooDateTime
		{
			get;
			set;
		}

	}

#if (!WindowsCE && !SILVERLIGHT)

	[TestFixture]
	public class GetBaseValidator
	{

		[Test]
		public void Should_Create_Web_Client_Validator_For_Regex_Validator()
		{
			var rule = new RegexValidator(null, false, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.None);

			var ruleContext = new RuleContext { Rule = rule };
			var list = BaseValidatorGenerator.Strategies.FindStrategy(ruleContext).CreateWebClientValidators(rule, null);
			Assert.IsNotNull(list);
			Assert.AreEqual(1, list.Count);
		}

	}
#endif
}
